package Test;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.jnlp.*;
import static javax.swing.JFrame.*;

public class WebStartDemo {
    final static JFrame frame = new JFrame( "WebStartDemo" );
    final static JPanel titlePanel = new JPanel();
    final static JTextArea fileNameLabel = new JTextArea( "<No File Selected>" );
    final static JPanel btnPanel = new JPanel();
    final static JTextArea fileTextArea = new JTextArea();

    public static void main ( String [] args ) {
        frame.setSize( 600, 400 );
        frame.setLocationRelativeTo( null );  // Center on screen
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

        // Draw the initial UI on the Event Dispatch Thread:
        try {
        SwingUtilities.invokeAndWait( new Runnable() {
            public void run () {
                drawUI( frame );
            }
        });
        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit( 1 );
        }
        frame.setVisible( true );
    }

    private static void drawUI ( JFrame frame ) {
        // Add and layout components:
        frame.setLayout( new BorderLayout() );
        frame.add( titlePanel, "North" );
        fileNameLabel.setFont( new Font("SanSerif", Font.BOLD, 18 ) );
        titlePanel.add( fileNameLabel );
        frame.add( new JScrollPane( fileTextArea ), "Center" );
        frame.add( btnPanel, "South" );
        JButton btn = new JButton( "Open File..." );
        btnPanel.add( btn );

        // Hook up Event handling:
        btn.addActionListener( new ActionListener() {
            public void actionPerformed ( ActionEvent ae ) {
                displayFile();
            }
        });
    }

    private static void displayFile () {
        FileOpenService fos;

        // Get reference to JNLP (WebStart) file open service:
        try {
            fos = (FileOpenService) ServiceManager.lookup(
                "javax.jnlp.FileOpenService" );
        } catch ( UnavailableServiceException e ) {
            fos = null;
        }

        if ( fos != null ) {
            try {
                // Ask user to select a text file through JNLP service:
                // (Note, JNLP may ignore the suggested extensions, so any
                // file might be returned.  TODO: Check file type is text.
                FileContents fileContents =
                   fos.openFileDialog( null, new String[] { "txt", "java" } );

                //  Update the file name label:
                String fileName = fileContents.getName();
                // JNLP may hide the file name, so:
                if ( fileName == null || fileName == "" )
                    fileName = "<Unknown file name>";
                fileNameLabel.setText( fileName );

                // Read and display file contents:
                InputStreamReader isr =
                    new InputStreamReader( fileContents.getInputStream() );
                fileTextArea.read( isr, null );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }
}
